package com.a100.equipment.controller;


import com.a100.equipment.entity.EquArchives;
import com.a100.equipment.entity.EquCategory;
import com.a100.equipment.entity.po.PageEquCategoryResponsePo;
import com.a100.equipment.entity.vo.*;
import com.a100.equipment.service.EquArchivesService;
import com.a100.utils.utils.R;
import com.a100.utils.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * <p>
 * 设备档案 前端控制器
 * </p>
 *
 * @author index
 * @since 2021-12-02
 */
@Api(description="设备档案前端控制器")
@RestController
@RequestMapping("/equipment/equ-archives")
public class EquArchivesController {
    @Autowired
    private EquArchivesService equArchivesService;

    @ApiOperation(value = "新建设备")
    @PostMapping("/")
    public R saveEquArchivesByVo(@ApiParam(name = "createCategoryVo", value = "新增设备档案类", required = true)
                                 @RequestBody CreateEquArchivesVo createEquArchivesVo){
        //调用逻辑层完成新增功能
        boolean b=equArchivesService.saveEquCategoryByVo(createEquArchivesVo);
        //判断新增完成与否
        if(b){
            //新增成功
            return R.ok().code(ResultCode.SUCCESS.code()).success(true).message("新增设备成功");
        }
        return R.ok().message("新增设备失败");
    }
    @ApiOperation(value = "修改设备")
    @PutMapping("/")
    public R updateEquArchivesByVo(@ApiParam(name = "createCategoryVo", value = "修改设备档案类", required = true)
                                   @RequestBody UpdateEquArchivesVo updateEquArchivesVo){
        //调用逻辑层完成修改功能
        boolean b=equArchivesService.updateEquCategoryByVo(updateEquArchivesVo);
        //判断新增完成与否
        if(b){
            //新增成功
            return R.ok().code(ResultCode.SUCCESS.code()).success(true).message("修改设备成功");
        }
        return R.ok().message("修改设备失败");
    }
    @ApiOperation(value = "启用和禁用设备")
    @PutMapping("/state")
    public R updateDisableenabledevice( @ApiParam(name = "QueryPlayersVo",value = "条件",required = true)
                                            @RequestBody UpdateDisableOrEnableArcvhives udea){
        //调用逻辑层完成启用和禁用设备功能
        boolean b=equArchivesService.updateDisableenabledevice(udea);
        //判断新增完成与否
        if(b){
            //新增成功
            return R.ok().code(ResultCode.SUCCESS.code()).success(true).message("配置成功");
        }
        return R.ok().message("配置设备失败");
    }
    @ApiOperation(value = "条件分页查询所有设备")
    @PostMapping("/{current}/{pagesize}")
    public R listPageByQuery(@ApiParam(name = "current",value = "当前页",required = true)
                             @PathVariable("current")Integer current,
                             @ApiParam(name = "pagesize",value = "每页数量",required = true)
                             @PathVariable("pagesize")Integer pagesize,
                             @ApiParam(name = "QueryPlayersVo",value = "条件")
                             @RequestBody PageEquCategoryVo pageEquCategoryVo){
        //获得分页组件并返回结果
        HashMap<String, Object> hashMap=equArchivesService.listPageByQuery(current,pagesize,pageEquCategoryVo);
        if(StringUtils.isEmpty(hashMap)){
            return R.error();
        }
        //返回集合给前端
        return  R.ok().message("查询数据成功").code(ResultCode.SUCCESS.code()).success(true).data(hashMap);
    }
    @ApiOperation(value = "根据id查询设备")
    @GetMapping("/{equArchivesId}")
    public R chooseEquArchivesById(@ApiParam(name = "equArchivesId", value = "类型的id", required = true)
                                   @PathVariable("equArchivesId")Long equArchivesId){
        PageEquCategoryResponsePo equArchives=equArchivesService.chooseEquArchivesById(equArchivesId);
        if(equArchives==null){
            return R.error().message("查询失败");
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("equArchives",equArchives);
        //返回集合给前端
        return R.ok().data(hashMap).code(ResultCode.SUCCESS.code()).success(true).message("查询成功");
    }
    @ApiOperation(value = "根据id设备")
    @DeleteMapping("/{equArchivesId}")
    public R deleteEquArchivesById(@ApiParam(name = "equArchivesId", value = "设备id", required = true)
                                     @PathVariable("equArchivesId")Long equArchivesId){
        //调用逻辑完成增加更新操作
        boolean b= equArchivesService.deleteEquArchivesById(equArchivesId);
        //判断回调结果
        if(!b){
            return R.error();
        }
        return R.ok().message("删除成功");
    }
    @ApiOperation(value = "通过Excel批量添加设备")
    @PostMapping("/excel")
    public R saveArchivesByExcel(@ApiParam(name = "file", value = "Excel文件", required = true)
                                              MultipartFile file){
        //文件不能为空
        if(StringUtils.isEmpty(file)){
            return R.error().message("没有文件内容");
        }
        //获得文件格式格式加上. 如.jpg
        String originalFilename = file.getOriginalFilename();
        int index = originalFilename.trim().lastIndexOf(".");
        String excel=originalFilename.trim().substring(index);
        //判定格式是否有误
        if(!".xlsx".equals(excel)&&!".xls".equals(excel)){
            return R.error().message("文件格式不符合");
        }
        boolean b=equArchivesService.saveArchivesByExcel(file,equArchivesService);
        if(!b){
            return R.error().message("文件操作失败");
        }
        //返回集合给前端
        return  R.ok().message("操作完成！");
    }
    @ApiOperation(value = "通过Excel批量导出设备")
    @GetMapping("/excel")
    public R exportArchivesByExcel(){
        //调用逻辑返回路径
        String excel = equArchivesService.exportArchivesByExcel();
        if(StringUtils.isEmpty(excel)){
            return R.error().message("文件操作失败");
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("excel",excel);
        //返回集合给前端
        return  R.ok().message("操作完成！").data(hashMap);
    }
}

